package cn.lili.modules.grab.xdxt;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GoodsSkuClient;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.grab.GrabOrderParam;
import cn.lili.modules.grab.enums.SourceEnums;
import cn.lili.modules.grab.factory.order.GrabOrderService;
import cn.lili.modules.grab.xdxt.reps.order.CreateOrderReps;
import cn.lili.modules.grab.xdxt.reps.order.OrderFreightPreviewReps;
import cn.lili.modules.grab.xdxt.res.order.CreateOrderReq;
import cn.lili.modules.grab.xdxt.service.XdxtOrderServie;
import cn.lili.modules.order.order.client.OrderItemClient;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GrabXdxtOrderServieImpl implements GrabOrderService {

    @Resource
    private XdxtOrderServie xdxtOrderServie;
    @Resource
    private GoodsSkuClient goodsSkuClient;
    @Resource
    private OrderItemClient orderItemClient;

    @Override
    public SourceEnums step() {
        return SourceEnums.XDXT_ORDER;
    }

    /**
     * 运费计算
     *
     * @param grabParam 请求内容
     */
    @Override
    public ResultMessage<Object> freightCalc(GrabOrderParam grabParam) {
        try {
            OrderFreightPreviewReps orderFreightPreviewReps = xdxtOrderServie.freightCalc(grabParam.getOrderFreightPreviewReq());
            return ResultUtil.data(orderFreightPreviewReps);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建订单
     *
     * @param grabParam 请求内容
     */
    @Override
    public ResultMessage<Object> createOrder(GrabOrderParam grabParam) {
        try {
            OrderItem orderItem = grabParam.getOrderItem();
            Order order = grabParam.getOrder();
            GoodsSku goodsSku = goodsSkuClient.getGoodsSkuById(orderItem.getSkuId());
            String[] address = order.getConsigneeAddressPath().split(",");

            CreateOrderReq.OrderDetailsList orderDetail = new CreateOrderReq.OrderDetailsList();
            orderDetail.setCode(goodsSku.getThirdPartyPlatformId());
            orderDetail.setNum(order.getGoodsNum());
            List<CreateOrderReq.OrderDetailsList> orderDetailsList = new ArrayList<>();
            orderDetailsList.add(orderDetail);

            //构建订单信息
            CreateOrderReq createOrderReq = new CreateOrderReq();
            createOrderReq.setPlatformOrderNo(orderItem.getOrderSn());
            createOrderReq.setCustName(order.getConsigneeName());
            createOrderReq.setCustMobile(order.getConsigneeMobile());
            createOrderReq.setProvince(address[0]);
            createOrderReq.setCity(address[1]);
            createOrderReq.setDistrict(address[2]);
            createOrderReq.setAddressDetail(order.getConsigneeDetail());
            createOrderReq.setOrderDetailsList(orderDetailsList);

            //下单
            CreateOrderReps createOrderReps = xdxtOrderServie.createOrder(createOrderReq);
            log.info("推送订单 #createOrder# 执行下单结果:{}", JSONObject.toJSONString(createOrderReps));
            //更新子订单
            orderItem.setOrderJsonVal(JSONObject.toJSONString(createOrderReps));
            orderItem.setOrderItemIdVal(String.valueOf(createOrderReps.getResult().getOrderList().get(0).getSuborderId()));
            orderItemClient.updateById(orderItem);
            return ResultUtil.data(createOrderReps);
        } catch (JsonProcessingException e) {
            log.info("推送订单 #createOrder# 异常", e);
            throw new RuntimeException(e);
        }
    }
}
